package com.zhourh.bang.common.support;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Response implements Serializable {

    private int code;

    private String message;

    private Map<String,Object> data;

    private List list;

    private Response(){}

    //static method

    public static Response instance(){
        Response instance =  new Response();
        instance.data = new HashMap<>();
        return instance;
    }

    public static Response instance(int code){
        return instance().code(code);
    }

    public static Response instance(String message){
        return instance().message(message);
    }

    public static Response instance(Map<String,Object> data){
        return instance().data(data);
    }

    public static Response success(){
        Response response = Response.instance();
        response.setCode(200);
        response.setMessage("success");
        return response;
    }

    public static Response success(int code){
        return success().code(code);
    }

    public static Response success(String message){
        return success().message(message);
    }

    public static Response success(Map<String,Object> data){
        return success().data(data);
    }

    public static Response fail(){
        Response response = Response.instance();
        response.setCode(500);
        response.setMessage("fail");
        return response;
    }

    public static Response fail(int code){
        return fail().code(code);
    }

    public static Response fail(String message){
        return fail().message(message);
    }

    public static Response fail(Map<String,Object> data){
        return fail().data(data);
    }


    // base method

    public Response code(int code){
        this.setCode(code);
        return this;
    }

    public Response message(String message){
        this.setMessage(message);
        return this;
    }

    public Response data(Map<String,Object> data){
        this.setData(data);
        return this;
    }

    public Response put(String key,Object value){
        this.data.put(key,value);
        return this;
    }

    public Response list(List list){
        this.setList(list);
        return this;
    }

    public Object get(String key){
        return this.data.get(key);
    }



    //getter && setter

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }
}
